<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index()
    {
        if(session('name')){
            $this->assign('username',session('name'));
            //实现对文章的分页查询
            $news = M('article');
            $count = $news->count();//查询表中总记录数
            $page = new \Think\Page($count,2);
            $page->setConfig('header',
                '<li class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
            $page->setConfig('prev','上一页');
            $page->setConfig('next','下一页');
            $page->setConfig('last','尾页');
            $page->setConfig('first','首页');
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $page->lastSuffx = false;//设置最后一页不显示为总页数
            $show=$page->show();
            $news_list = $news->
            field(array('id','cid','wzname','author','createtime','lasttime'))
                ->order('id desc')
                ->limit($page->firstRow.','.$page->listRows)
                ->select();
            $news_list = $this->filter($news_list);
            $this->assign('title','新闻后台管理');
            $this->assign('news_count',$count);
            $this->assign('news_list',$news_list);
            $this->assign('pages',$show);
            $this->display();

        }else{
            //当用户没有登录时，跳转到登录页面
            $this->error("请先登录",U('/Admin/Glogin/index/'));
        }

    }
    public function  filter($param){
//        var_dump($param);
//        exit();
        foreach ($param as $key => $value){
            $param[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            if(!$value['lasttime']){
                $param[$key]['lasttime'] = '无';
            }else{
                $param[$key]['lasttime'] = date('Y-m-d H:i:s',$value['lasttime']);
            }
            if(mb_strlen($value['wzname'],'utf-8') >8){
                $param[$key]['wzname'] = mb_substr($value['wzname'],0,8,'utf-8')."......";
            }
//            var_dump($value);
//            exit();
        }
        return $param;
    }
    public function quit(){
        if(session('name')){
            session(null);
        }

        redirect(U('/Admin/Glogin/index/'),0,"重新登录");
    }
    public function actual(){
        redirect(U("/Admin/Actual/index"));
    }
    public function article(){
        if(session('name')){
            redirect(U('/Admin/Article/index/'),0,"写文章");
        }else{
            redirect(U('/Admin/Login/index/'),0,"重新登录");
        }

    }
    public function edit($id){
        if(session('name')){
            redirect(U('/Admin/Article/edit/',array('id'=>$id)),0,"修改文章");
        }else{
            redirect(U('/Admin/Login/index/'),0,"重新登录");
        }

    }
    public function delete($id){
        $article=M('article');
        $flag = $article->where('`id`='.$id)->delete();
        if($flag){
            $this->success("删除成功",U('/Admin/Index/index/'));
        }else{
            $this->error("删除失败");
        }
    }
}